import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:convert';
import 'package:get/get.dart';
import 'package:flutter_html/flutter_html.dart';

class DioDemoNewsDetail extends StatefulWidget {
  const DioDemoNewsDetail({Key? key}) : super(key: key);

  @override
  _DioDemoNewsDetailState createState() => _DioDemoNewsDetailState();
}

class _DioDemoNewsDetailState extends State<DioDemoNewsDetail> {
  List _list = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _getData();
  }

  _getData() async {
    var apiUrl =
        "https://www.phonegap100.com/appapi.php?a=getPortalArticle&aid=${Get.arguments["aid"]}";
    print(apiUrl);

    var response = await Dio().get(apiUrl);

    setState(() {
      _list = json.decode(response.data)["result"];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("新闻详情"),
      ),
      body: _list.isNotEmpty
          ? ListView(
              children: [
                Text("${_list[0]["title"]}",
                    textAlign: TextAlign.center,
                    style: const TextStyle(fontSize: 26)),
                Padding(
                  padding: const EdgeInsets.all(10),
                  child: Html(
                    data: _list[0]["content"],
                    style: {
                      "body": Style(backgroundColor: Colors.white),
                      "p": Style(fontSize: FontSize.large),
                    },
                    onLinkTap: (url, _, __) {
                      print("Opening1 $url");
                    },
                    onAnchorTap: (url, _, __) {
                      print("Opening2 $url");
                    },
                  ),
                )
              ],
            )
          : const Center(child: CircularProgressIndicator()),
    );
  }
}
